Skip to content

Add RegisterNatives overload for raw function pointers#1391

Merged
jonathanpeppers merged 4 commits intomainfrom
dev/simonrozsival/register-natives-intptr
Mar 19, 2026
Merged

Add RegisterNatives overload for raw function pointers#1391
jonathanpeppers merged 4 commits intomainfrom
dev/simonrozsival/register-natives-intptr

Conversation

@simonrozsival
Copy link
Member

Add JniNativeMethod struct matching JNI's JNINativeMethod layout (byte* Name, byte* Signature, IntPtr FunctionPointer) and a new RegisterNatives(JniObjectReference, ReadOnlySpan<JniNativeMethod>) overload.

Calls the JNI RegisterNatives function pointer directly via the function table, bypassing delegate marshaling entirely. Zero allocations in the entire path.

Add end-to-end test with a dedicated Java class (RegisterNativesTestType) and an [UnmanagedCallersOnly] native callback that is registered via the new API, then called from Java to verify the result.

Add JniNativeMethod struct matching JNI's JNINativeMethod layout
(byte* Name, byte* Signature, IntPtr FunctionPointer) and a new
RegisterNatives(JniObjectReference, ReadOnlySpan<JniNativeMethod>)
overload.

Calls the JNI RegisterNatives function pointer directly via the
function table, bypassing delegate marshaling entirely. Zero
allocations in the entire path.

Add end-to-end test with a dedicated Java class (RegisterNativesTestType)
and an [UnmanagedCallersOnly] native callback that is registered via
the new API, then called from Java to verify the result.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 18, 2026 12:37
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

##[error]src\Java.Interop\Java.Interop\JniEnvironment.Types.cs(288,99): Error CS1061: 'JniEnvironmentInfo' does not contain a definition for 'Invoker' and no accessible extension method 'Invoker' accepting a first argument of type 'JniEnvironmentInfo' could be found (are you missing a using directive or an assembly reference?)
D:\a\1\s\src\Java.Interop\Java.Interop\JniEnvironment.Types.cs(288,99): error CS1061: 'JniEnvironmentInfo' does not contain a definition for 'Invoker' and no accessible extension method 'Invoker' accepting a first argument of type 'JniEnvironmentInfo' could be found (are you missing a using directive or an assembly reference?) [D:\a\1\s\src\Java.Interop\Java.Interop.csproj]
##[error]src\Java.Interop\Java.Interop\JniNativeMethodRegistration.cs(30,23): Error RS0016: Symbol 'implicit constructor for 'JniNativeMethod'' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
D:\a\1\s\src\Java.Interop\Java.Interop\JniNativeMethodRegistration.cs(30,23): error RS0016: Symbol 'implicit constructor for 'JniNativeMethod'' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md) [D:\a\1\s\src\Java.Interop\Java.Interop.csproj]
##[error]src\Java.Interop\Java.Interop\JniNativeMethodRegistration.cs(36,10): Error RS0016: Symbol 'JniNativeMethod' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
D:\a\1\s\src\Java.Interop\Java.Interop\JniNativeMethodRegistration.cs(36,10): error RS0016: Symbol 'JniNativeMethod' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md) [D:\a\1\s\src\Java.Interop\Java.Interop.csproj]
##[error]src\Java.Interop\PublicAPI.Unshipped.txt(16,1): Error RS0017: Symbol 'Java.Interop.JniNativeMethod.JniNativeMethod(byte* name, byte* signature, System.IntPtr functionPointer) -> void' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
D:\a\1\s\src\Java.Interop\PublicAPI.Unshipped.txt(16,1): error RS0017: Symbol 'Java.Interop.JniNativeMethod.JniNativeMethod(byte* name, byte* signature, System.IntPtr functionPointer) -> void' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md) [D:\a\1\s\src\Java.Interop\Java.Interop.csproj]

simonrozsival and others added 3 commits March 19, 2026 09:09
- Fix CS1061: Use JNIEnv function table directly via (*((JNIEnv**)env))
  instead of info.Invoker which doesn't exist in the function pointers
  compilation path
- Fix RS0016: Add implicit parameterless constructor to PublicAPI
- Fix RS0016/RS0017: Use nint instead of System.IntPtr in PublicAPI
  (matches .NET 10 representation)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The Java class was not listed in the JavaInteropTestJar item group,
causing ClassNotFoundException at runtime.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jonathanpeppers jonathanpeppers merged commit 6363926 into main Mar 19, 2026
2 checks passed
@jonathanpeppers jonathanpeppers deleted the dev/simonrozsival/register-natives-intptr branch March 19, 2026 12:33
@simonrozsival simonrozsival added the copilot `copilot-cli` or other AIs were used to author this label Mar 19, 2026
jonathanpeppers added a commit to dotnet/android that referenced this pull request Mar 24, 2026
…avaConvert (#10967)

Fixes: #10791

## Summary

Adds the runtime-side support for the trimmable typemap: type resolution, peer creation, native method registration, and AOT-safe collection marshaling. All behind `RuntimeFeature.TrimmableTypeMap` (defaults to `false`).

### New runtime types
- **`JavaPeerProxy` / `JavaPeerProxy<T>`** — AOT-safe proxy attribute base. Generated proxy types extend this and provide `CreateInstance()` for peer creation and `GetContainerFactory()` for collection marshaling.
- **`IAndroidCallableWrapper`** — `RegisterNatives(JniType)` interface for ACW proxy types to register JNI native methods.
- **`JavaPeerContainerFactory`** — AOT-safe factories for arrays, lists, collections, and dictionaries without `MakeGenericType()`.
- **`TrimmableTypeMap`** — Central class owning the `TypeMapping` dictionary. Encapsulates all proxy attribute access: peer creation, invoker resolution, container factories, and native method registration bootstrap.
- **`TrimmableTypeMapTypeManager`** — `JniTypeManager` subclass delegating type lookups to `TrimmableTypeMap`. `RegisterNativeMembers` throws `UnreachableException` (JCW static blocks handle registration).

### Type resolution
- `GetProxyForManagedType(Type)` resolves managed type → JNI name via `IJniNameProviderAttribute` (shared interface for `[Register]` and `[JniTypeSignature]`) → TypeMap dictionary → proxy. Results cached in `ConcurrentDictionary`.
- `TryGetJniNameForType` shared between `TrimmableTypeMap` and `TrimmableTypeMapTypeManager`.
- `ActivateInstance` uses JNI `GetObjectClass` → `GetJniTypeNameFromClass` → TypeMap lookup for constructor activation (proxy types have self-applied attribute, not the target type).

### TypeMap dictionary initialization
- The runtime's `GetOrCreateExternalTypeMapping<Java.Lang.Object>()` handles assembly resolution automatically via the `System.Runtime.InteropServices.TypeMappingEntryAssembly` runtimeconfig property (set in `Trimmable.targets`). No manual `Assembly.Load` pre-loading is needed — the runtime walks `TypeMapAssemblyTargetAttribute` attributes from the entry assembly recursively.

### Native interop refactoring
- **`RegisterJniNatives` passed via init args** — `Initialize` sets `args->registerJniNativesFn` (null when `TrimmableTypeMap=true`). Eliminates the `create_delegate` call for `RegisterJniNatives`, letting the trimmer remove it cleanly in the trimmable path.
- **C++ `registerNatives` stub** — `Host::Java_mono_android_Runtime_registerNatives` no-op for the trimmable path (managed code handles registration via `Initialize`).
- **Null guard** on `jnienv_register_jni_natives` call site.
- `Initialize` is now the **single** `create_delegate` entry point from native code.

### RegisterNatives bootstrap
- `mono.android.Runtime.registerNatives(Class)` Java native method added
- `TrimmableTypeMap.Initialize()` registers the JNI callback during init (behind explicit `if (RuntimeFeature.TrimmableTypeMap)` guard for trimmer compatibility)
- When Java loads a JCW class, `OnRegisterNatives` resolves the proxy and calls `IAndroidCallableWrapper.RegisterNatives()` to bind UCO function pointers

### AOT-safe JavaConvert
- `JavaConvert.GetJniHandleConverter()` uses `JavaPeerContainerFactory` for `IList`, `ICollection`, `IDictionary`
- `JNIEnv.ArrayCreateInstance()` uses factory path

### Peer creation
- `JavaMarshalValueManager` (renamed from `ManagedValueManager`) overrides `CreatePeer` for the trimmable path — calls `proxy.CreateInstance()` directly, bypassing `GetUninitializedObject`
- `GetSimpleReferences` walks base type chain for managed-only subclasses without `[Register]`

### Wiring
- `RuntimeFeature.TrimmableTypeMap` feature switch with ILLink substitutions
- `JNIEnvInit` (CoreCLR) and `JavaInteropRuntime` + `JreRuntime` (NativeAOT) create the new managers when the feature is on
- `JavaMarshalValueManager` (renamed from `ManagedValueManager`) gets proxy-based peer creation in `TryConstructPeer`

### Dependencies
- dotnet/java-interop#1391 (RegisterNatives with raw function pointers — follow-up optimization)
- Manifest generator: #10991
- Build pipeline: #10980

### Test coverage
- 255/255 generator tests pass
- End-to-end HelloWorld app runs on device with Activity, layout XML, FindViewById\<Button\>, Click events
- Runtime tests require device integration (tracked by #10793)

### Trimmer size regression prevention
- Add ILLink substitutions for `IsMonoRuntime` and `IsCoreClrRuntime` — enables trimmer to eliminate runtime-specific branches
- `JavaMarshalValueManager`: remove `TrimmableTypeMap?` field, use `TrimmableTypeMap.Instance` singleton
- `TrimmableTypeMap`: private ctor, `Initialize()` static method, `Instance` throws if not initialized
- `TrimmableTypeMapTypeManager`: no constructor parameter, uses singleton
- No `TrimmableTypeMap` type references outside feature guards — clean trimming for MonoVM/NativeAOT

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Jonathan Peppers <jonathan.peppers@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

copilot `copilot-cli` or other AIs were used to author this

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants